<template>
  <!--<el-button color="#626aef" :dark="isDark" plain icon="Download" @click="handleOut"
    >导出</el-button
  >-->
  <div class="home-box">
    <el-form :inline="true" :model="PageForm">
      <el-form-item label="员工姓名">
        <el-input
          v-model="PageForm.empName"
          placeholder="员工姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="创建时间" style="width: 18.75rem">
  		  
       
       <el-date-picker
         v-model="PageForm.time"
         type="month"
         placeholder="选择月"
       		value-format="YYYY-MM"
       		:disabledDate="check">
       </el-date-picker>
  		
      </el-form-item>
      <el-form-item>
        <el-button @click="searchEvent" type="primary">查询 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="update" type="primary">重置 </el-button>
      </el-form-item>
      <el-form-item >
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button circle icon="Refresh" @click="update" />
        </el-tooltip>
      </el-form-item>
    </el-form>
  </div>
  <el-table
    :data="KpiRuleList"
    border
    style="width: 95%; margin: 20px 0px; border-radius: 8px;box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);"
    :header-cell-style="{
      background: '#337ecc !important',
      color: '#ffffff',
      border: 'none !important',
      textAlign: 'center',
    }"
    :cell-style="{ textAlign: 'center' }"
  >
    <el-table-column label="员工姓名" prop="empName" width="110" class-name="my-content"></el-table-column>
    <el-table-column label="服务地区" prop="region" width="110" class-name="my-content"></el-table-column>
    <el-table-column label="地区系数" prop="regionCoefficient" width="110" class-name="my-content"></el-table-column>
    <el-table-column label="岗位系数" prop="positionCoefficient" width="110" class-name="my-content"></el-table-column>
    <el-table-column label="基础工资/￥" prop="wage" width="110" class-name="my-content"></el-table-column>
    <el-table-column label="绩效工资/￥" prop="performanceWage" width="110" class-name="my-content"></el-table-column>
    <el-table-column
      prop="state"
      label="状态"
      width="70"
    >
      <template #default="scope">
        <el-tag
          :type="scope.row.tag === 'Home' ? '' : 'success'"
          disable-transitions
          >{{ scope.row.state === 0 ? "未启用" : "启用" }}</el-tag
        >
      </template>
    </el-table-column>
    <el-table-column label="最后更新时间">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.updateTime }}</span>
        </div>
      </template>
    </el-table-column>
  </el-table>
  <pagination
    class="pagination"
    v-show="KpiListPage.total > 0"
    :total="KpiListPage.total"
    v-model:page="KpiListPage.page"
    v-model:limit="KpiListPage.page_size"
    @pagination="getListData"
  />
</template>
<script setup>
import {
  ref,
  onMounted,
  onBeforeMount,
  reactive,
  defineEmits,
  watch,
} from "vue";
import { getCoefficientPastPage ,getCoefficientPastSearch} from "@/api/coefficient";
import { ElMessage } from "element-plus";

const PageForm = ref({
  page: 1,
  page_size: 10,
  time:"",
  empName:"",
});
const KpiListPage = ref([]);
const KpiRuleList = ref([]);
onBeforeMount(() => {
  getCoefficientPastPage(PageForm.value).then((data) => {
    KpiListPage.value = data.data;
    KpiRuleList.value = data.data.records;
  });
});
watch(
  KpiRuleList,
  (newVal) => {
    
  },
  { deep: true }
);

const searchEvent=()=>{
	getCoefficientPastSearch(PageForm.value).then(data=>{
      KpiListPage.value = data.data;
      KpiRuleList.value = data.data.records;
    })
}


const update = (form) => {
	PageForm.value.empName=""
  getCoefficientPastPage(PageForm.value).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "更新成功",
        type: "success",
      });
      KpiListPage.value = data.data;
      KpiRuleList.value = data.data.records;
    } else {
      ElMessage.error("更新失败");
    }
  });
};

const handlePagination=(value)=>{
  if(PageForm.value.empName==""){
    getCoefficientPastPage(value).then(data=>{
        KpiListPage.value = data.data;
        KpiRuleList.value = data.data.records;
    })
  }
  else{
    PageForm.value.page=value.page;
    PageForm.value.page_size=value.page_size
    getCoefficientPastSearch(PageForm.value).then(data=>{
     KpiListPage.value = data.data;
     KpiRuleList.value = data.data.records;
    })
  } 
}

const now = new Date();
const year = now.getFullYear();
// 获取当前月份（注意：月份从0开始，所以需要加1）
const month = now.getMonth() == 0? 12:now.getMonth();
// // 格式化为 YYYY-MM 形式
const yearMonth = `${year}-${month < 10 ? '0' : ''}${month}`;

PageForm.value.time = yearMonth

const check = (time)=>{
	 return time.getTime() > new Date(new Date().toLocaleDateString()).getTime() - 31 * 24 * 3600 * 1000;
}
watch(PageForm.value, (newValue, oldValue) => {
	searchEvent()
}, { deep: true });

</script>

<style lang="scss" scoped>
.title1 {
  color: rgb(64, 158, 255);
  width: 350px;
  height: 47px;
  font-size: 25px;
  background: rgba(64, 158, 255, 0.4);
  text-align: center;
  border-radius: 10px;
  margin: 10px auto 10px;
  line-height: 47px;
  border-top: #409eff 2px solid;
  border-bottom: #409eff 2px solid;
  box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
}
.pagination {
  margin: 20px 50px;
  display: flex;
  justify-content: end;
}
</style>